/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.pagetransformer;

import com.to.aboomy.banner.BannerViewPager;
import ohos.agp.components.Component;

/**
 * 旋转动画
 *
 * @since 2021-08-09
 */
public class RotateDownPageTransformer implements BannerViewPager.PageTransformer {
    private static final float ANGLE = 15f;
    private static final float NUM_F_0_6 = 0.6f;
    private static final float NUM_F_2_5 = 2.5f;

    @Override
    public void transformerPage(BannerViewPager.PageType pageType, Component page, float offset, float offsetPixels) {
        float angle = ANGLE;
        float height = page.getHeight();
        switch (pageType) {
            case PREVIOUS:
                if (offsetPixels <= 0) {
                    float pivotX = page.getWidth() - page.getWidth() * offset * NUM_F_0_6;
                    page.setPivot(pivotX, height);
                    angle = -ANGLE * (1 - offset);
                } else {
                    angle = -ANGLE;
                }
                break;
            case CURRENT:
                // 往下一页滑入
                if (offsetPixels > 0) {
                    float pivotX = page.getWidth() / NUM_F_2_5 + page.getWidth() * offset * NUM_F_0_6;
                    page.setPivot(pivotX, height);
                    angle = -(ANGLE * offset);
                } else {
                    float pivotX = page.getWidth() - (page.getWidth() / NUM_F_2_5)
                            - page.getWidth() * offset * NUM_F_0_6;
                    page.setPivot(pivotX, height);
                    angle = ANGLE * offset;
                }
                break;
            case NEXT:
                if (offsetPixels >= 0) {
                    page.setPivot(page.getWidth() * offset * NUM_F_0_6, height);
                    angle = ANGLE * (1 - offset);
                }
                break;
            case OTHER:
                if (offsetPixels > 0) {
                    page.setPivot(0, height);
                    angle = ANGLE;
                } else {
                    page.setPivot(page.getWidth(), height);
                    angle = -ANGLE;
                }
                break;
            default:
                break;
        }
        page.setRotation(angle);
    }
}
